﻿package KK.System
{
	import KK.Controls.Skin.ISkinSheet;
	import KK.net.QueueLoader;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	

	public final class System
	{ 
		//////////////////////////////////////////////////////////////////////
		static private var AUTO_INCREMENT:int=0;
		static private function get AI():int{
			return AUTO_INCREMENT++;
		}
		static public const DEBUG:int=1;
		static public const NORMAL:int=0;
		static public const LOCAL:int=AI;
		static public const NETWORK:int=AI;
		///////////////////////////////////////////////////////////////////////
		
		static public const VERSION:int=0x00020000;//1.0版本
		
		static public var Environment:int=LOCAL;//运行环境 
		
		static public const FRAME_RATE:uint=60;//系统帧频 
		
		
		static private var _loader:QueueLoader=new QueueLoader(10);
		static private var _amf:QueueLoader=new QueueLoader(1);
		static protected var _debug:int=DEBUG;

		public function System()
		{
		}
		
		
		static public function AddSwfLoader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=undefined,_SecurityError:Function=undefined,_Progress:Function=undefined):void{
//			_loader=QueueLoader.GetInstance(10);
			var id:int=_loader.AddSwfLoader(_url,_Complete,_params,_IOError,_SecurityError,_Progress); 
		}
		
		
		static public function UpLoader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=undefined,_SecurityError:Function=undefined,contentType:String=""):void{
//			_loader=QueueLoader.GetInstance(10);
			_loader.UpLoader(_url,_Complete,_params,_IOError,_SecurityError,contentType);
		}
		
		
		static public function AddLoader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=undefined,_SecurityError:Function=undefined,contentType:String=""):void{
//			_loader=QueueLoader.GetInstance(10);
			_loader.AddLoader(_url,_Complete,_params,_IOError,_SecurityError,contentType);
		}
		
		
		static public function AddAMFLoader(_url:String,_path:String,_Complete:Function,_IOError:Function=undefined,_SecurityError:Function=undefined,..._params):void{
//			_loader=QueueLoader.GetInstance(10);
			_amf.AddAMFLoader.apply(null,[_url,_path,_Complete,_IOError,_SecurityError].concat(_params));
		}
		
		
		static public function AddBytesLoader(byte:ByteArray,_Complete:Function,name:String="",_IOError:Function=undefined,_SecurityError:Function=undefined,_Progress:Function=undefined):void
		{
//			_loader=QueueLoader.GetInstance(10);
			_loader.AddBytesLoader(byte,_Complete,name,_IOError,_SecurityError,_Progress);
		}
		 
		static public function CleanMC(child:DisplayObjectContainer):void
		{
			var i:int=0;
			for(i=child.numChildren-1;i>=0;i--)
			{
				child.removeChildAt(i);
			}
		}
		static public function DebugMSG(...content):void{ 
			if(System._debug==DEBUG)
			{
				var msg:String="";
				var i:int=0;
				
				for(i=0;i<content.length;i++)
				{
					msg+= content[i]+" ";
				}
				Debug.AddMSG(msg);
				System.Trace(msg);
			}
		}
		static protected function Trace(msg:String):void
		{
			trace(msg);
		}
		
		
		static public function set debug(value:int):void{
			//0为debug 模式 ，1为非debug 模式
			System._debug=value;
			
		}
		static public function get debug():int{
			return System._debug;
		}
		
		
		/**
		 * 返回16进制字符  
		 * @return 
		 * 
		 */
		static public function ByteToHEX(bytes:ByteArray):String{
			var result:String="";
			var ix:int=0;
			var lenx:int=bytes.length;
			var ex:int=bytes.position;
			
			bytes.position=0;
			while(ix<lenx){
				result+=uint(bytes.readUnsignedByte()).toString(16) +"  ";
				ix++;
			}
			return result;
		}
		
		
		/**
		 * 绘制皮肤 
		 * @param sourceBitmapData
		 * @param target
		 * @param skin
		 * 
		 */
		static public function FillSheetSkin(source:BitmapData,target:BitmapData,skin:ISkinSheet):void{
			FillVectorBitmapData(source,target,skin.Area,skin.targetRect);
		}
		
		
		/**
		 * 填充数组数据 
		 * @param sourceBitmapData
		 * @param target
		 * @param sourceArea
		 * @param targetArea
		 * 
		 */
		static public function FillVectorBitmapData(sourceBitmapData:BitmapData,target:BitmapData,sourceArea:Vector.<Rectangle>,targetArea:Vector.<Rectangle>):void{
			var i:int=0;
			const len:int=sourceArea.length;
			if(len!=targetArea.length){
				LogError("绘图数量不对");
			}
			while(i<len){
				fillBitmapData(sourceBitmapData,target,sourceArea[i],targetArea[i]); 
				i++;
			}
		}
		
		/**
		 * 位图填充  
		 * @param sourceBitmapData 填充源图片
		 * @param target		填充目标图片
		 * @param sourceArea	填充源区域
		 * @param target		填充目标区域
		 * 
		 */
		static public function fillBitmapData(sourceBitmapData:BitmapData,target:BitmapData,sourceArea:Rectangle,targetArea:Rectangle):void{
			var expoint:Point=new Point(targetArea.x,targetArea.y);
			var targetSize:Point=new Point(targetArea.width+targetArea.x,targetArea.height+targetArea.y); 
			var bool:Boolean=false;
			for(;;){
				switch(true){ 
					case expoint.x<targetSize.x:
						//填充宽度    
						expoint.x=expoint.x+sourceArea.width>targetSize.x?targetSize.x-sourceArea.width:expoint.x;
						target.copyPixels(sourceBitmapData,sourceArea,expoint);
						expoint.x+=sourceArea.width;
						
						break;
					case expoint.y<targetSize.y:
						//换行并且调整位置
						expoint.x=targetArea.x;
						expoint.y+=sourceArea.height;
						break; 
					
				}
				if(expoint.y>=targetSize.y){
					return ;
				}
			}
		}
		
		
	}
}